﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmClock : FrmDefault
    {
        public FrmClock()
        {
            InitializeComponent();
        }

        private void BntOK_Click(object sender, EventArgs e)
        {
            if (BntOK.Text == ResourcesText.WordOK)
            {
                this.Close();
            }
            this.Cursor = Cursors.WaitCursor;
            if (!SharedLib.ModemGSM.Online)
            {
                this.Cursor = Cursors.Default;
                SharedLib.MsgAlert(ResourcesText.MsgErrOffLine);
                return;
            }
            SharedLib.StartWaitProcessThread();
            bool Ok = SharedLib.ModemGSM.ClockDateTimeWrite();
            if (Ok)
            {
                string aux = SharedLib.ModemGSM.ReadClockDate();
                string[] part = aux.Split('/');
                if (aux != "")
                {
                    string DtFtm = Properties.Settings.Default.DefaultDateFmt;
                    DtFtm = DtFtm.Replace("DD", part[2]);
                    DtFtm = DtFtm.Replace("MM", part[1]);
                    DtFtm = DtFtm.Replace("YYYY", part[0]);
                    LblDate.Text = ResourcesText.WordDate + " " + DtFtm;
                    aux = SharedLib.ModemGSM.ReadClockTime();
                    part = aux.Split(':');
                    if (aux != "")
                    {
                        LblTime.Text = ResourcesText.WordTime + " " + part[0] + ":" + part[1] + ":" + part[2];
                    }
                    PctInf.Image = Properties.Resources.CelPhone;
                    Lblmsg.Visible = true;
                    Lblmsg.Text = ResourcesText.DateTimeSave;
                    timerClock.Enabled = true;
                    BntEnd.Enabled = false;
                    BntOK.Text = ResourcesText.WordOK;
                }
                else
                {
                    Lblmsg.Visible = true;
                    Lblmsg.Text = ResourcesText.WordError;
                }
            }
            else
            {
                Lblmsg.Visible = true;
                Lblmsg.Text = ResourcesText.WordError;
            }
            this.Cursor = Cursors.Default;
            SharedLib.EndWaitProcessThread();
        }

        private void FrmClock_Load(object sender, EventArgs e)
        {
            Lblmsg.Visible = false;
        }

        private void timerClock_Tick(object sender, EventArgs e)
        {
            DateTime dtref = System.DateTime.Now;
            string[] part = dtref.ToString("yy/MM/dd").Split('/');
            string strDate = Properties.Settings.Default.DefaultDateFmt;
            strDate = strDate.Replace("DD", part[2]);
            strDate = strDate.Replace("MM", part[1]);
            strDate = strDate.Replace("YYYY", part[0]);
            LblDate.Text = strDate;
            LblTime.Text = dtref.ToString("HH:mm:ss");
            if (timerClock.Interval < 1000)
            {
                timerClock.Interval = 1000;
            }
        }

        private void BntEnd_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FrmClock_FormClosing(object sender, FormClosingEventArgs e)
        {
            timerClock.Stop();
        }
    }
}
